package users

import (
	"go_auth/ext/settings"
	"go_auth/utils/commons"
	"go_auth/utils/mysql"
)

type UserSource struct {
	Id       int64
	Uid      string `xorm:" varchar(100) 'uid'"`
	SourceId string `xorm:" varchar(100) 'source_id'"`
}

func ExistUserSourceByUid(uid string) string {
	userSource := new(UserSource)
	_, err := settings.MasterDB.Where("uid=?", uid).Get(userSource)

	if !commons.CheckErr(err) {
		return settings.Empty
	}
	return userSource.SourceId
}

func InsertUserSource(uid, sourceId string) bool {
	userSource := UserSource{Uid: uid, SourceId: sourceId}

	s := settings.MasterDB.NewSession()

	var err error
	if err = s.Begin(); err != nil {
		return false
	}
	defer s.Rollback()
	if !mysql.CheckErrInsert(s, userSource) {
		return false
	}
	s.Commit()
	s.Close()
	return true
}
